package demo.db.elasticSearch.highClient;

import java.io.IOException;

import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.DocWriteResponse.Result;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.support.replication.ReplicationResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

/**
 * <h1>Java High Level REST Client</h1>从6.0.0开始加入的，目的是以java面向对象的方式来进行请求、响应处理。
 * 每个API 支持 同步/异步 两种方式，同步方法直接返回一个结果对象。异步的方法以async为后缀，通过listener参数来通知结果。
 * 
 * 兼容性说明：依赖 java1.8 和 Elasticsearch core project,请使用与服务端ES版本一致的客户端版本
 * 
 * @author hanjy
 *
 */
public class DeleteDemo {

    private static Logger logger = LoggerFactory.getLogger(DeleteDemo.class);

    @Test
    public void deleteById() {
        String index = "slow_redis_index.2021-04-10";
        String id = "zXWpuXgBB6-iknr5aWDE";
        DeleteRequest deleteRequest = new DeleteRequest(index);
        deleteRequest.id(id);

        try (RestHighLevelClient client = RestHighLevelClientDemo.getClient()) {
            DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
            System.err.println(deleteResponse);
            System.err.println("DeleteResponse : " + JSON.toJSONString(deleteResponse, true));
            Result result = deleteResponse.getResult();
            System.err.println(result);
        } catch (IOException e) {
            logger.error("", e);
        }
    }

    @Test
    public void deleteDemo() {
        try (RestHighLevelClient client = RestHighLevelClientDemo.getClient()) {
            // 1、创建获取文档请求
            DeleteRequest request = new DeleteRequest("mess", "_doc", "33");
            // request.routing("routing");
            // request.parent("parent");
            // Timeout to wait for primary shard to become available
            request.timeout(TimeValue.timeValueMinutes(2));
            // request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
            // request.version(2);
            // request.versionType(VersionType.EXTERNAL);
            // 3、发送请求
            // 同步请求
            DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
            if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
                logger.warn("not found");
            }
            String index = deleteResponse.getIndex();
            String type = deleteResponse.getType();
            String id = deleteResponse.getId();
            long version = deleteResponse.getVersion();
            ReplicationResponse.ShardInfo shardInfo = deleteResponse.getShardInfo();
            if (shardInfo.getTotal() != shardInfo.getSuccessful()) {

            }
            if (shardInfo.getFailed() > 0) {
                for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
                    String reason = failure.reason();
                }
            }
        } catch (IOException e) {
            logger.error("", e);
        }
    }

}
